<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="text" version="1.0" encoding="US-ASCII" indent="yes"/>
    <xsl:output method="html" encoding="{document('definitions.xml')//charset}" indent="yes" name="htmlFormat"/>
    
<!--   ________________________________________________________________

        File: xml2bib.xsl (this version of xml2bib.xsl is ONLY for
                           use with Xsltproc !)
        Version: 1.03 (private)
        Date: 2002-September-05
        Author: Michael Auth;
                Developed developed at the Department of Linguistics
                of the University of Tuebingen, Germany.               
                This stylefile is based upon on a stylefile from
                Brenno Lurati and Luca Previtali from the former
                bibtexml.org project by the ETH Zuerich.
        
        Copyright (C) 2002 Michael Auth
        ________________________________________________________________
        
        This stylefile is distributed under the terms of the
        GNU GENERAL PUBLIC LICENSE:
        This program is free software; you can redistribute it and/or
        modify it under the terms of the GNU General Public License
        as published by the Free Software Foundation; either version 2
        of the License, or (at your option) any later version.
        
        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
        See the file COPYING.txt (which comes along with the package file
        xml2html.zip) for the full liscence text about the terms and
        conditions for copying, distributing and modification.
        ________________________________________________________________

              
        Info: This stylesheet does...
        *****************************
        - ... transform the XML file back into a BibTeX-file.
          Since the XML file replaces your BibTeX-file, all 
          modifications will be applied to the new XML-file.
          With the generated BibTex-file you can use your
          bibliography with LaTeX.
        - ... generate single HTML-files (one for each entry), showing
          the entry in its originally BibTeX format - for easy
          copy and paste the entry in another BibTeX-file.
          All these files are linked with the BibTeX image with
          the corresponding entry in list.html and all category sorted HTML-files.
          -> This function is used by the stylesheet xml2html.xsl
             (using the <xsl:include> element)!
          
        !! DO NOT MODIFY THIS STYLESHEET.
        IN CASE OF MODIFYING THE CATEGORIES AND OTHER VARIABLES SEE DEFINITIONS.XML !!
-->

<!-- Save the root node of the basic XML file (so it won't mix up with
     "definitions.xml", which will be included later): -->
    <xsl:variable name="main" select="/"/>

<!-- Set the bibhtml_path parameter:
     This parameter sets the bib_html path (the path were all
     BibTeX-HTML files will be stored to -->
    <xsl:variable name="bibhtml_path" select="'bibhtml/'"/>

    
<!-- ###################### Main Template - Start Parsing ################# -->
<xsl:template match="/">
    <!-- First, proceed BibTeX-comment/macro/preamble: -->
        <xsl:for-each select="//comment">
            <xsl:text>%</xsl:text>
            <xsl:apply-templates/>
            <xsl:text>%</xsl:text>
            <xsl:text>&#10;</xsl:text>
        </xsl:for-each>
        <xsl:for-each select="//macro">
            <xsl:text>@string{</xsl:text>
            <xsl:apply-templates select="@alias"/>
            <xsl:text> = {</xsl:text>
            <xsl:apply-templates/>
            <xsl:text> }}</xsl:text>
            <xsl:text>&#10;</xsl:text>
        </xsl:for-each>
        <xsl:for-each select="//preamble">
            <xsl:text>@preamble{{</xsl:text>
            <xsl:apply-templates/>
            <xsl:text>}}</xsl:text>
            <xsl:text>&#10;</xsl:text>
        </xsl:for-each>
        <xsl:text>&#10;</xsl:text>
        
    <!-- Now, proceed all entries: -->
        <xsl:call-template name="all"/>
</xsl:template>


<!-- ################## Single BIBTEX-FILE Template ####################### -->
<!-- proceed ALL BibTeX entries to one single BibTeX-file: -->
<xsl:template name="all">
    <xsl:for-each select="//bibliography/bibitem">
        <xsl:call-template name="write_content_bib"/>
    </xsl:for-each>    
</xsl:template>
<!-- ################## Single BibTeX-file - END - ######################## -->



<!-- ###################### HTML-FILES Template ########################### -->
<!-- This templated is called by the <xsl:include> element from the
     stylesheet xml2html.xsl!
     This template writes each BibTeX entry to an external
     html-file, which is linked with the BibTeX-image of each
     entry in the main html-files.
     The name of the file is set by the label of the BibTeX-entry.
     Note: All this files will be stored to a path "bibhtml/".
-->
<xsl:template name="bibtex_html">
    <!-- select every single XML entry -->
    <xsl:for-each select="//bibliography/bibitem">
    
        <xsl:variable name="filename">
            <xsl:value-of select="$bibhtml_path" />
            <xsl:value-of select="@label" />
        </xsl:variable>

        <!-- use <xsl:document> to force output to an external html-file: -->
        <xsl:document href="{$filename}.html" format="htmlFormat">
            <html>
            <head>
                <title>
                    <xsl:value-of select="document('definitions.xml')//title_bar"/>
                </title>
                
                <!-- Check if there is an external CSS-file defined in definitions.xml;
                     If yes, load information from the external file.
                     If no, proceed with the following predefined CSS style infomation: -->
                <xsl:choose>
                    <xsl:when test="document('definitions.xml')//css_filename !=''">
                        <!-- Check if there is a 'bibhtml_path' parameter (argument given);
                             If yes, the css-file will be found in the root directory ("../").
                             If no, css-file can be found in the main directory: -->
                        <xsl:choose>
                            <xsl:when test="$bibhtml_path !=''">
                                <link rel="stylesheet" type="text/css" href="../{document('definitions.xml')//css_filename}" />
                            </xsl:when>
                            <xsl:otherwise>
                                <link rel="stylesheet" type="text/css" href="{document('definitions.xml')//css_filename}" />
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:when>
                    <xsl:otherwise>
                        <style type='text/css'>
                            body { background-color:#ffffff; }

                            .header { font-family:Arial, Helvetica, sans-serif; font-size:16pt; }
                            .small_head { font-family:Arial, Helvetica, sans-serif; font-size:11pt; }
                        </style>
                    </xsl:otherwise>
                </xsl:choose>
            </head>
            
            <body>
                <!--header - BEGIN-  -->
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr style="background-color:#fff0d0;">
                        <!-- Check if there is a logo defined in definitions.xml;
                             If yes, print it left from the header: -->
                        <xsl:if test="document('definitions.xml')//logo !=''">
                            <td width="20%" height="30" valign="middle" align="left">
                                <img src="{concat('../', concat(document('definitions.xml')//pic_path, document('definitions.xml')//logo))}" alt="logo" />
                            </td>
                        </xsl:if>
                        <td width="80%" height="30" valign="middle" align="left">
                            <div class="header">
                                <xsl:value-of select="document('definitions.xml')//head_bibtex"/>
                            </div>
                        </td>
                        <td width="20%" height="30" valign="middle" align="right">
                            <div class="small_head">
                                <a href="javascript:history.back()">
                                    <xsl:text>Back to list</xsl:text>
                                </a>
                            </div>
                        </td>
                    </tr>
                </table>
                <!--header - END - -->
                
                <hr />
                <br />
                
                <!-- Write entry-data - BEGIN - -->
                <pre>
                    <xsl:call-template name="write_content_bib"/>
                </pre>
                <!-- Write entry-data - END - -->
                
                <br />
            </body>
            </html>
        </xsl:document>
    </xsl:for-each>
</xsl:template>
<!-- ################## HTML-FILES Template - END - ####################### -->




<!-- ######################### Entries Template ########################### -->
<!-- BibTeX entries + content of corresponding fields: -->
<xsl:template name="write_content_bib">
    
    <!-- write: @EntryType{citKey, -->
    <xsl:text>@</xsl:text>
    <xsl:value-of select="@type"/><xsl:text>{</xsl:text>
    
    <!-- Check, if there were any colons replaced by another character
         inside the BibTeX label field; 
         If yes, restore the original colon and write the label; 
         If no; just write the label: -->
    <xsl:choose>
        <xsl:when test="document('definitions.xml')//bibtex_label/replace[@org_char != '']">
            <xsl:value-of select="translate(@label, document('definitions.xml')//bibtex_label/replace/@new_char, document('definitions.xml')//bibtex_label/replace/@org_char)"/>
            <xsl:text>,</xsl:text>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="@label"/>
        </xsl:otherwise>
    </xsl:choose>
    
    <xsl:text>&#10;</xsl:text>

    <!-- write: author = {name(s)}, -->
    <xsl:if test="author">
        <xsl:text>  author&#9;= </xsl:text>
        <!-- check if author name(s) is/are represented by an abbrev. -->
        <xsl:if test="author/abbrev">
            <xsl:apply-templates select="author/abbrev"/>
            <xsl:text>,</xsl:text>
        </xsl:if>
        <xsl:if test="not(author/abbrev)">
            <xsl:text>{</xsl:text>
            <xsl:apply-templates select="author" mode="bib" />
            <xsl:text>},</xsl:text>
        </xsl:if>
        <xsl:text>&#10;</xsl:text>
    </xsl:if>
    
    <!-- write: editor = {name(s)}, -->
    <xsl:if test="editor">
        <xsl:text>  editor&#9;= </xsl:text>
        <!-- check if editor name(s) is/are represented by an abbrev. -->
        <xsl:if test="editor/abbrev">
            <xsl:apply-templates select="editor/abbrev"/>
            <xsl:text>,</xsl:text>
        </xsl:if>
        <xsl:if test="not(editor/abbrev)">
            <xsl:text>{</xsl:text>
            <xsl:apply-templates select="editor" mode="bib" />
            <xsl:text>},</xsl:text>
        </xsl:if>
        <xsl:text>&#10;</xsl:text>
    </xsl:if>
            

    <!-- write: all other fields: entryField = {content}, -->
    <xsl:for-each select="./*">
        <xsl:choose>
            <xsl:when test="local-name(.)='label'">
            </xsl:when>
            <xsl:when test="local-name(.)='author'">
            </xsl:when>
            <xsl:when test="local-name(.)='editor'">
            </xsl:when>
            <xsl:when test="local-name(.)='notstandard'">
                <!-- Check if the current notstandard field is set for print-out
                     in definitions.xml;
                     If yes write the field and its content;
                     If no, omit field -->
                <xsl:variable name="nst_name" select="./@name" />
                <xsl:if test="document('definitions.xml')//notst_fields[notst_print=$nst_name]">
                    <xsl:call-template name="notstandard"/>
                </xsl:if>
            </xsl:when>

            <!-- force output for all other fields: -->
             <xsl:otherwise>
                <xsl:text>  </xsl:text>
                <xsl:value-of select="local-name(.)"/>
                <xsl:if test="string-length(local-name(.)) &lt; 6">
                    <xsl:text>&#9;</xsl:text>
                </xsl:if>
                <xsl:text>&#9;= </xsl:text>
                <!-- check if content of field is represented by an abbrev. -->
                <xsl:if test="not(./abbrev)">
                    <xsl:text>{</xsl:text>
                    <xsl:apply-templates/>
                    <xsl:text>}</xsl:text>
                </xsl:if>
                <xsl:if test="./abbrev">
                    <xsl:apply-templates select="./abbrev"/>
                </xsl:if>
                <xsl:if test="not(position()=last())">
                    <xsl:text>,</xsl:text>
                </xsl:if>
                <xsl:text>&#10;</xsl:text>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:for-each>
    <xsl:text>}&#10;&#10;</xsl:text>
</xsl:template>
<!-- ######################### Entries - END - ############################ -->



<!-- ###################### notstandard Template ########################## -->
<xsl:template name="notstandard">
    <!-- force output of notstandard field only for those set in DEFINITIONS.XML.
        ALL OTHER NOTSTANDARD FIELDS WILL BE IGNORED
        AND NOT PRINTED IN THE BIBTEX-HTML FILE!!! -
         ==> If you want to force output for other notstandard fields,
             see DEFINITIONS.XML for modifications!
-->
    <xsl:text>  </xsl:text>
      <xsl:value-of select="@name"/>
      <xsl:if test="string-length(@name) &lt; 6">
          <xsl:text>&#9;</xsl:text>
      </xsl:if>
      <xsl:text>&#9;= </xsl:text>
      <!-- check if content of notstandard field is represented by an abbrev. -->
      <xsl:choose>
          <xsl:when test="./abbrev">
              <xsl:apply-templates select="./abbrev"/>
              <xsl:text>&#10;</xsl:text>
          </xsl:when>
          <xsl:otherwise>
              <xsl:text>{</xsl:text>
              <xsl:value-of select="normalize-space(.)"/>
              <xsl:text>}</xsl:text>
              <xsl:if test="not(position()=last())">
                  <xsl:text>,</xsl:text>
              </xsl:if>
              <xsl:text>&#10;</xsl:text>
          </xsl:otherwise>
      </xsl:choose>
</xsl:template>
<!-- ###################### notstandard - END - ########################### -->



<!-- ##################### authors/editors Template ####################### -->
<xsl:template match="author" mode="bib">
     
    <!-- LastName: -->
    <xsl:apply-templates select="lastname" mode="bib"/>
    
    <!-- FirstName: -->
    <xsl:if test="firstname">
        <xsl:text>,</xsl:text>
        <xsl:apply-templates select="firstname" mode="bib"/>
    </xsl:if>

    <!-- MiddleName(s): -->
    <xsl:if test="middlename">
        <xsl:apply-templates select="middlename" mode="bib"/>
    </xsl:if>

    <!-- Check if there are more authors:
         if true, place "and" between them: -->
    <xsl:if test="not(position()=last())">
        <xsl:text> and </xsl:text>
    </xsl:if>

</xsl:template>


<xsl:template match="editor" mode="bib">
     
    <!-- LastName: -->
    <xsl:apply-templates select="lastname" mode="bib"/>
    
    <!-- FirstName: -->
    <xsl:if test="firstname">
        <xsl:text>,</xsl:text>
        <xsl:apply-templates select="firstname" mode="bib"/>
    </xsl:if>

    <!-- MiddleName(s): -->
    <xsl:if test="middlename">
        <xsl:apply-templates select="middlename" mode="bib"/>
    </xsl:if>

    <!-- Check if there are more authors:
         if true, place "and" between them: -->
    <xsl:if test="not(position()=last())">
        <xsl:text> and </xsl:text>
    </xsl:if>

</xsl:template>


<xsl:template match="lastname" mode="bib">
    <xsl:if test="contains(.,' ') and not(starts-with(.,'v')) and not(starts-with(.,'d'))">
        <xsl:text>{</xsl:text>
    </xsl:if>
    <xsl:apply-templates/>
    <xsl:if test="contains(.,' ') and not(starts-with(.,'v')) and not(starts-with(.,'d'))">
        <xsl:text>}</xsl:text>
    </xsl:if>
</xsl:template>

<xsl:template match="firstname" mode="bib">
    <xsl:text> </xsl:text>
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="middlename" mode="bib">
    <xsl:text> </xsl:text>
    <xsl:apply-templates/>
</xsl:template>
<!-- ##################### authors/editors - END - ######################## -->


<xsl:template match="abbrev">
    <xsl:value-of select="@alias"/>
</xsl:template>

</xsl:stylesheet>
